/********************************************************/
/*	Project:					*/
/*	Author:		gong_libin			*/
/*	Date:		2016_01_01			*/
/*	File:		GlbTest.cpp			*/
/********************************************************/

#include "GlbMongo.h"

void GlbInsert(CGlbMongo& rCMongo)
{
	UINT uiCount = 0;
	char szName[GLB__BUFFER] = { 0 };
	char szCity[GLB__BUFFER] = { 0 };
	char szArea[GLB__BUFFER] = { 0 };
	char szMail[GLB__BUFFER] = { 0 };
	char szTele[GLB__BUFFER] = { 0 };
	bson_t stQuery = BSON_INITIALIZER;

	/* db.test.insert(
  		{id:0,
  		'name':'zhang001',
		'city':'beijing001',
		'area':'chaoyang001',
		'mail':'my_email001',
		...}
	) */
	while (uiCount < 10) {
		sprintf(szName, "zhang%03d", uiCount);
		sprintf(szCity, "beijing%03d", uiCount);
		sprintf(szArea, "chaoyang%03d", uiCount);
		sprintf(szMail, "my_email%03d", uiCount);
		sprintf(szTele, "15800000%03d", uiCount);
		BSON_APPEND_INT64(&stQuery, "id", uiCount);
		BSON_APPEND_UTF8(&stQuery, "name", szName);
		BSON_APPEND_UTF8(&stQuery, "city", szCity);
		BSON_APPEND_UTF8(&stQuery, "area", szArea);
		BSON_APPEND_UTF8(&stQuery, "mail", szMail);
		BSON_APPEND_UTF8(&stQuery, "tele", szTele);
		if (true != rCMongo.GlbCollInsert(&stQuery)) {
			GLB__ERROR("Failed to GlbCollInsert\n");
		}
		bson_reinit(&stQuery);
		uiCount += 1;
	}

	bson_destroy(&stQuery);

	return;
}

void GlbSelect(CGlbMongo& rCMongo)
{
	char* pszRec = NULL;
	bson_t* pstDoc = NULL;
	bson_t stQuery = BSON_INITIALIZER;

	/* db.test.find(
  		{'name':"zhang005'}
	) */
	BSON_APPEND_UTF8(&stQuery, "name", "zhang003");
	rCMongo.GlbCollSelect(&stQuery, NULL);
	if (true == rCMongo.GlbCursorMore() && NULL != (pstDoc = rCMongo.GlbCursorNext())) {
		pszRec = bson_as_json(pstDoc, NULL);
		GLB__PRINT("Record: %s\n", pszRec);
		bson_free(pszRec);
	}
	else {
		GLB__ERROR("Not found\n");
	}
	rCMongo.GlbDestroyCursor();

	return;
}

void GlbDelete(CGlbMongo& rCMongo)
{
	bson_t stQuery = BSON_INITIALIZER;

	/* db.test.remove(
  		{'name':'zhang009'}
	) */
	BSON_APPEND_UTF8(&stQuery, "name", "zhang009");
	rCMongo.GlbCollSelect(&stQuery, NULL);
	if (true != rCMongo.GlbCollDelete(&stQuery)) {
		GLB__ERROR("Failed to GlbCollDelete\n");
	}
	rCMongo.GlbDestroyCursor();

	return;
}

void GlbUpdate(CGlbMongo& rCMongo)
{
	bson_t stField = BSON_INITIALIZER;
	bson_t stSelect = BSON_INITIALIZER;
	bson_t stUpdate = BSON_INITIALIZER;

	/* db.test.update(
  		{'name':'zhang006'},
		{$set:{'name':'xxxxxxxx','city':'xxxxxxxxxx'}},
		false,
		false
	) */
	bson_append_document_begin(&stUpdate, "$set", -1, &stField);
	BSON_APPEND_UTF8(&stField, "name", "xxxxxxxx");
	BSON_APPEND_UTF8(&stField, "city", "xxxxxxxxxx");
	bson_append_document_end(&stUpdate, &stField);

	BSON_APPEND_UTF8(&stSelect, "name", "zhang006");
	if (true != rCMongo.GlbCollUpdate(&stSelect, &stUpdate)) {
		GLB__ERROR("Failed to GlbCollUpdate\n");
	}

	bson_destroy(&stSelect);
	bson_destroy(&stUpdate);
	bson_destroy(&stField);

	return;
}

void GlbClean(CGlbMongo& rCMongo)
{
	/* db.test.remove({}) */
	rCMongo.GlbCollClean();

	return;
}

void GlbDrop(CGlbMongo& rCMongo)
{
	/* db.test.drop */
	rCMongo.GlbDropCollection();

	return;
}

void GlbDisplay(CGlbMongo& rCMongo)
{
	char* pszRec = NULL;
	bson_t* pstDoc = NULL;

	rCMongo.GlbCollTraverse();
	while (true == rCMongo.GlbCursorMore() && NULL != (pstDoc = rCMongo.GlbCursorNext())) {
		pszRec = bson_as_json(pstDoc, NULL);
		GLB__PRINT("Record: %s\n", pszRec);
		bson_free(pszRec);
	}
	rCMongo.GlbDestroyCursor();

	return;
}

void GlbPrint(CGlbMongo& rCMongo)
{
	bson_iter_t stIter;
	bson_t* pstDoc = NULL;

	rCMongo.GlbCollTraverse();
	while (true == rCMongo.GlbCursorMore() && NULL != (pstDoc = rCMongo.GlbCursorNext())) {
		if (true == bson_iter_init(&stIter, pstDoc)) {
			while (true == bson_iter_next(&stIter)) {
				GLB__PRINT("%s: ", bson_iter_key(&stIter));
				switch (bson_iter_type(&stIter)) {
					case BSON_TYPE_UTF8:
						GLB__PRINT("%s, ", bson_iter_utf8(&stIter, NULL));
						break;
					case BSON_TYPE_INT64:
						GLB__PRINT("%lld, ", bson_iter_int64(&stIter));
						break;
					default:
						break;
				}
			}
			GLB__PRINT("\n");
		}
		else {
			break;
		}
	}
	rCMongo.GlbDestroyCursor();

	return;
}

int main(int argc, char* argv[])
{
	CGlbMongo CMongo;

	CMongo.GlbMongoInit();
	if (GLB__SUCCESS == CMongo.GlbGetClient((char*)"mongodb://127.0.0.1/")) {
		if (GLB__SUCCESS == CMongo.GlbGetCollection((char*)"test", (char*)"test")) {
			GlbInsert(CMongo);
			GlbDisplay(CMongo);

			GlbSelect(CMongo);
			GlbPrint(CMongo);

			GlbUpdate(CMongo);
			GlbDisplay(CMongo);

			GlbDelete(CMongo);
			GlbDisplay(CMongo);

			GlbClean(CMongo);
			GlbDrop(CMongo);

			CMongo.GlbDestroyCollection();
		}
		else {
			GLB__ERROR("Failed to GlbGetCollection\n");
		}
		CMongo.GlbDestroyClient();
	}
	else {
		GLB__ERROR("Failed to GlbGetClient\n");
	}

	CMongo.GlbMongoClean();

	return GLB__SUCCESS;
}
